package JDBC_learn;

import JDBC_learn_Object.Account_1;
import Program.Account;
import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class JDBCDemo5 {
    @Test
    public void testResultSet() throws Exception{
        //1.注册驱动
        //  Class.forName("com.mysql.jdbc.Driver");
        //2.获取链接
        // String url="jdbc:mysql://127.0.0.1:3306/itheima";
        //如果连接的是本机的myaql并且端口号是默认的3306可以简化书写
        String url="jdbc:mysql:///itheima?useSSL=false";
        String username="root";
        String password="11235813";
        Connection conn = DriverManager.getConnection(url, username, password);
        //3.定义sql
        String sql="select*from account";
        //4.获取statement对象
        Statement stmt = conn.createStatement();
        //5.执行sql
        ResultSet rs = stmt.executeQuery(sql);
        //6.处理结果，遍历rs中的所有数据
        List<Account_1>list=new ArrayList<>();
        while (rs.next()){
            Account_1 account=new Account_1();
            int id = rs.getInt("id");
            String name=rs.getString("name");
            int money=rs.getInt("money");
            account.setId(id);
            account.setName(name);
            account.setMoney(money);
            list.add(account);
        }
        System.out.println(list);
        rs.close();
        stmt.close();
        conn.close();
    }
}
